/*
 *The code is written by NaLi, All rights reserved.
 */
package com.nali.dal.statement.function.formatter;

import java.util.List;

import org.springframework.stereotype.Component;

import com.nali.dal.statement.function.DataHandler;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionException;

/**
 * @author gavin 
 * Created on 2011-4-12
 */
@Component
public class Range implements DataHandler<Integer>{

	@Override
	public Integer handle(List<Object> argList) throws FunctionException {
		double num = ((Number) argList.get(0)).doubleValue();
		double from = ((Number) argList.get(1)).doubleValue();
		double to = ((Number) argList.get(2)).doubleValue();
		int value = ((Number) argList.get(3)).intValue();
		int other = ((Number) argList.get(4)).intValue();
		
		if(from <= num && (num <= to || to == -1)) {
			return Integer.valueOf(value);
		}else{
			return Integer.valueOf(other);
		}
	}

	@Override
	public void validate(List<FunctionArgument> arguments) throws FunctionException {
		if(arguments == null || arguments.size() != 5) {
			throw new FunctionException("Invalid arguments: " + arguments);
		}
	}

}

